const Service = require('./index')
const { articleCommentModel, circleCommentModel } = require('@/models/comment.model')

class CommentService extends Service {
  async findPage (pageNo = 1, pageSize = 20, searchKeywords, type) {
    const model = type === 'article' ? articleCommentModel : circleCommentModel
    try {
      const { list, total } = await model.findPage({
        pageNo,
        pageSize,
        conditions: {
          like: {
            'user.nickname': searchKeywords,
            'comment.content': searchKeywords
          }
        }
      })
      return this.skipPage(list.map(item => {
        return {
          ...item,
          picList: item.picList?.split(';').filter(Boolean)
        }
      }), total, pageNo, pageSize)
    } catch (error) {
      throw new Error(error)
    }
  }

  async delete (type, id) {
    const model = type === 'article' ? articleCommentModel : circleCommentModel
    try {
      const exist = await model.exist(id)
      if (!exist) {
        return {
          errorMessage: '评论不存在'
        }
      }
      await model.delete(id)
    } catch (error) {
      throw new Error(error)
    }
  }
}

module.exports = new CommentService()
